package com.aponisipsis.gl3d.render;


import java.awt.geom.CubicCurve2D;
import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.glu.GLU;

import util.Memory;

public class Light{
	FloatBuffer diffuse;
	FloatBuffer ambient;
	FloatBuffer specular;
	FloatBuffer position;
	int id;
	
	public Light(int lightId){
		this.id = lightId;
		diffuse = Memory.floatBuffer(4);
		ambient = Memory.floatBuffer(4);
		specular = Memory.floatBuffer(4);
		position = Memory.floatBuffer(4);
	}
	
	public void setDiffuse(float r, float g, float b, float a){
		diffuse.put(new float[]{r,g,b,a}).flip();
	}
	
	public void setAmbient(float r, float g, float b, float a){
		ambient.put(new float[]{r,g,b,a}).flip();
	}
	
	public void setSpecular(float r, float g, float b, float a){
		specular.put(new float[]{r,g,b,a}).flip();
	}
	
	public void setPosition(float x, float y, float z){
		position.clear();
		position.put(new float[]{x,y,z,1}).flip();
	}
	
	public static Light white(int lightId){
		Light light = new Light(lightId);
		light.setAmbient(.1f,.1f,.1f, 1);
		light.setDiffuse(0.8f, 0.8f, 0.8f, 1);
		light.setSpecular(1, 1, 1, 1);
		
		return light;
	}

	public static Light red(int lightId){
		Light light = new Light(lightId);
		light.setAmbient(.2f, 0, 0, 1);
		light.setDiffuse(0.6f, 0.0f, 0.0f, 1);
		light.setSpecular(1, 0,0, 1);
		
		return light;
	}
	
	public void render() {
		GL11.glEnable(GL11.GL_LIGHTING);
		GL11.glEnable(id);	// Enable the light (GL_LIGHT1 - 7)
        
        
		GL11.glLight(id, GL11.GL_DIFFUSE, diffuse);   // color of the direct illumination
        GL11.glLight(id, GL11.GL_SPECULAR, specular); // color of the highlight
        GL11.glLight(id, GL11.GL_AMBIENT, ambient);   // color of the reflected light
        GL11.glLight(id, GL11.GL_POSITION, position);
        
        //GL11.glLightf(GLLightHandle, GL11.GL_QUADRATIC_ATTENUATION, .005F);    // how light beam drops off
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
        GL11.glTranslatef(position.get(0), position.get(1), position.get(2));
        
        //GL11.glColor3f(1, 1, 0);
        //new org.lwjgl.util.glu.Sphere().draw(1, 8, 8);
        
        
	}

}
